//
//  RedAddViewController.m
//  项目A_百思不得姐
//
//  Created by wudongyang on 16/5/16.
//  Copyright © 2016年 wudongyang. All rights reserved.
//

#import "RedAddViewController.h"
#import "MyButton.h"

#define WH  100
#define TOP 350
#define CR  3

@interface RedAddViewController ()
{
    int btnIndex;
}
@property(nonatomic,retain)NSMutableArray *btnArray;

@property (nonatomic, strong) NSTimer *timer;

@property(nonatomic, strong)UIButton *QXButton;

@end

@implementation RedAddViewController
-(void)viewWillAppear:(BOOL)animated{
         btnIndex = 0;
        // 定时器开启,每隔0.1秒让一个按钮开始动画
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.btnArray = [NSMutableArray array];
    UIImageView *imageV = [[UIImageView alloc] init];
    imageV.backgroundColor = [UIColor redColor];
    imageV.image = [UIImage imageNamed:@"hanbj.jpg"];
    imageV.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:imageV];
    
    UIImageView *wzimageV = [[UIImageView alloc] init];
    wzimageV.backgroundColor = [UIColor whiteColor];
    wzimageV.image = [UIImage imageNamed:@"hanwz.jpg"];
    wzimageV.frame = CGRectMake(50, 150, self.view.frame.size.width - 100, 30);
    [imageV addSubview:wzimageV];
    
    
    [self createButtonWithTitle:@"发视频" andImage:[UIImage imageNamed:@"publish-video"] andIndex:0];
    [self createButtonWithTitle:@"发图片" andImage:[UIImage imageNamed:@"publish-picture"] andIndex:1];
    [self createButtonWithTitle:@"发段子" andImage:[UIImage imageNamed:@"publish-text"] andIndex:2];
    [self createButtonWithTitle:@"发声音" andImage:[UIImage imageNamed:@"publish-audio"] andIndex:3];
    [self createButtonWithTitle:@"审帖" andImage:[UIImage imageNamed:@"publish-review"] andIndex:4];
    [self createButtonWithTitle:@"发链接" andImage:[UIImage imageNamed:@"publish-offline"] andIndex:5];
    self.QXButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.QXButton.frame = CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50);
    self.QXButton.backgroundColor = [UIColor clearColor];
    [self.QXButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.QXButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:self.QXButton];
    [self.QXButton addTarget:self action:@selector(QXButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    // Do any additional setup after loading the view.
}


//每0.1秒执行一次
- (void)timeChange
{

    // 如果当前 btnIndex值为数组的个数就停止
    if (btnIndex == self.btnArray.count) {
        [_timer invalidate];
        return;
    }
    //取出数组中的每个按钮
    UIButton *btn = self.btnArray[btnIndex];
    
    //    usingSpringWithDamping 弹簧效果 值越小弹簧效果越明显
    //    initialSpringVelocity  初始速度就是动画刚开始的速度
    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        // 还原所有按钮坐标到原始位置
        btn.transform = CGAffineTransformIdentity;
        
    } completion:nil];
    
    
    
    
    btnIndex++;
}


-(void)createButtonWithTitle:(NSString *)title andImage:(UIImage *)image andIndex:(int)index
{
    CGFloat margin = (self.view.frame.size.width - 3 * WH) / (CR + 1);
    
    
    MyButton *button  = [MyButton buttonWithType:UIButtonTypeCustom];
    
    [button setTitle:title forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    
    [button setImage:image forState:UIControlStateNormal];
    
    // CR是列数  下标%3决定x的坐标  下标/3决定y的坐标
    CGFloat btnX = index % CR * (WH + margin) + margin;
    CGFloat btnY = TOP + index / CR * (WH + margin);
    
    button.frame = CGRectMake(btnX, btnY, WH, WH);
    
    //    button.transform = CGAffineTransformMakeTranslation(0, self.view.bounds.size.height);
    
    // 设置每个按钮偏移位置
    button.transform = CGAffineTransformTranslate(button.transform, 0, self.view.bounds.size.height);
    
    [self.view addSubview:button];
    [self.btnArray addObject:button];
    
}
-(void)QXButtonAction:(UIButton *)button{
    
    [_timer invalidate];
    _timer = nil;

[self dismissViewControllerAnimated:YES completion:^{
    
    
}];
}



@end
